package com.itheima.core.controller;

import com.itheima.core.po.User;
import com.itheima.core.service.iservice.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class UserController {
    @Autowired
    private UserService userservice;
    @RequestMapping(value = "/login.action",method = RequestMethod.POST)
    public String login(String usercode, String password, Model model, HttpSession session, HttpServletRequest request){
        System.out.println(request.getRequestURI());
        User user = userservice.findUser(usercode,password);
        System.out.println(user);
        if(user != null){
            session.setAttribute("USER_SESSION",user);
            return "redirect:customer/list.action";
        }
        model.addAttribute("msg","账号或密码错误，请重新输入");
        return "login";
    }

    @RequestMapping("/toCustomer.action")
    public String toCustomer(){
        return "customer";
    }

    @RequestMapping("/logout.action")
    public String logout(HttpSession session){
        session.invalidate();
        return "redirect:login.action";
    }

}
